import { makeAutoObservable } from 'mobx'
import { getNoticeList } from '../api'
import type { NoticeType, NoticeListParams } from '../api/interface'

class Store {
    noticeList: NoticeType[] = []
    listTotal: number = 0
    listParams: NoticeListParams = {
        page: 1,
        per_page: 10,
    }
    hasMore: boolean = false
    constructor() {
        makeAutoObservable(this)
        this.fetchList()
    }
    async fetchList(search?: boolean) {
        //search 区分是否需要合并原数据
        let params = { ...this.listParams }
        const res = await getNoticeList(params)
        if (res.data) {
            const { total = 0, list = [] } = res.data
            this.noticeList = search ? list : [...this.noticeList, ...list]
            this.listTotal = total
            this.hasMore = total > 0 && this.noticeList.length < total
        }
    }
    setParams = (params: Partial<NoticeListParams> & { search?: boolean }) => {
        const { search = false } = params
        this.listParams = {
            ...this.listParams,
            ...params,
        }
        this.fetchList(search)
    }
    resetParams = () => {
        this.listParams = {
            page: 1,
            per_page: 10,
        }
        this.fetchList(true)
    }
}

export default Store
